package com.adtech.adms.commons.web.intercepter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.adtech.adms.Constants;
import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.util.BeanLoader;
import com.adtech.adms.commons.web.listener.OnlineUserListener;
import com.adtech.adms.system.LogConstants;
import com.adtech.adms.system.pojo.User;
import com.adtech.core.Settings;
import com.adtech.core.exception.ErrorCode;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.web.action.BaseAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;

/**
 * 
 * 进行权限控制
 * @author Dormin 
 * @since 2011-6-2 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class AuthInterceptor extends BaseInterceptor {

	private static final long serialVersionUID = 1L;

	private static final String JUMP_TO_LOGIN = "jumptologin";
	private static final String TEXT_IP_BLOCK = "session.ip.block";

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest httpRequest = ServletActionContext.getRequest();
		Map<String,Object> session = super.getSession(invocation);
		User user = (User)session.get(SessionKey.SESSION_KEY_USER);
		BaseAction action = (BaseAction) invocation.getAction();
		
		if("true".equals(Settings.get("login.five"))){	
			//屏蔽登录失败超过5次的ip，并记录日志
			String ip = getIpAddr(httpRequest);
			if(OnlineUserListener.checkNeedBlock(ip)) {
				action.addActionError(action.getText(TEXT_IP_BLOCK));
//				SystemLogService systemLogService = (SystemLogService) BeanLoader.getBean(Constants.SERVICE_SYSTEM_LOG);
				User tempUser = new User();
				tempUser.setLoginIp(ip);
//				systemLogService.addLog(LogConstants.LOG_TYPE_LOGIN_FAIL,new Object[]{ip},tempUser);
				return Action.LOGIN;
			}
		}

		//系统处在开发阶段，默认以管理员登陆方便调试
		/*
		if (user == null){
			UserService userService = (UserService)BeanLoader.getBean("userService");
			FeatureService featureService = (FeatureService)BeanLoader.getBean("featureService");
			user = userService.getUser(1);
			user.setFeaturesTree(featureService.getFeatureListByUser(user));
			httpRequest.getSession().setAttribute(SessionKey.SESSION_KEY_USER, user);
		}
		 */
//		if(user==null) {
//			String actionName = super.getActionName(invocation);
//			if(!Constants.LOGIN_ACTION_NAME.equals(actionName) && !Constants.LOGOUT_ACTION_NAME.equals(actionName)){
////				String errorMessage = (String) session.get(SessionKey.INVALID_REASON);
////				if(errorMessage!=null){
////					action.setErrorMessage(errorMessage);
////					session.put(SessionKey.INVALID_REASON, null);
////				}
//				if(iaAjaxRequest(invocation)) {
//					throw new ServiceException(ErrorCode.SESSION_TIMEOUT);
//				}
//				else {
//					return JUMP_TO_LOGIN;
//				}
//			}
//		}
		
		
			
		
		return invocation.invoke();
	}
	
	/**
	 * 获取真实的ip地址
	 * @param request
	 * @return 真实的ip地址
	 * @author Dormin
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
